package com.showdoc.controller;

import com.github.pagehelper.PageHelper;
import com.showdoc.mapper.UserMapper;
import com.showdoc.pojo.Template;
import com.showdoc.pojo.User;
import com.showdoc.service.TemplateService;
import com.showdoc.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * (Template)表控制层
 *
 * @author makejava
 * @since 2023-04-12 09:42:35
 */
@RestController
@RequestMapping("template")
public class TemplateController {
    /**
     * 服务对象
     */
    @Autowired
    private TemplateService templateService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询
     *
     * @param
     * @param template 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public R<ArrayList<Template>> queryAllByLimit(Template template, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum,5);
        ArrayList<Template> templateList = new ArrayList<>();
        List<Template> templates = templateService.queryAllByLimit(template);
        for (Template template1 : templates) {
            templateList.add(template1);
        }
        R<ArrayList<Template>> success = R.success(templateList);
        success.setCode(templateList.size());
        success.setData(templateList);
        return success;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R<Template> queryById(@PathVariable("id") Long id) {
        return R.success(templateService.queryById(id));
    }

    /**
     * 创建新公告
     *
     * @param template 实体
     * @return 新增结果
     */
    @PostMapping("/addTemplate")
    public R addTemplate(@RequestBody Template template,HttpServletRequest request ) {
        //获取当前用户
        Long id = (Long)request.getSession().getAttribute("user");
        User user = userMapper.selectByUid(id);
        //创建新公告
        Timestamp time = new Timestamp(System.currentTimeMillis());
        if(templateService.queryById(template.getId()) != null){
            return R.error("公告已存在，请重新输入....");
        }else{
            template.setUid(user.getUid());
            template.setUsername(user.getUsername());
            template.setAddtime(time.getTime());
        }
        //判断是否保存成功并返回信息
        boolean save = templateService.save(template);
        if (save) {
            return R.success(template);
        } else {
            return R.error("新建失败");
        }
    }

    /**
     * 编辑数据
     *
     * @param template 实体
     * @return 编辑结果
     */
    @PutMapping("/editTemplate")
    public R editTemplate(@RequestBody Template template,HttpServletRequest request) {
        //获取当前用户
        Long id = (Long)request.getSession().getAttribute("user");
        User user = userMapper.selectByUid(id);
        //更新公告
        Timestamp time = new Timestamp(System.currentTimeMillis());
        if(templateService.queryById(template.getId()) == null){
            return R.error("此公告不存在");
        }
        Template template1 = new Template();
        template1.setId(template.getId());
        template1.setUid(template.getUid());
        template1.setUsername(template.getUsername());
        template1.setTemplateTitle(template.getTemplateTitle());
        template1.setTemplateContent(template.getTemplateContent());
        template1.setAddtime(time.getTime());
        boolean update = templateService.update(template1);
        if (update) {
            return R.success(template1);
        } else {
            return R.error("新建失败");
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete")
    public R<Boolean> deleteById(Long id) {
        return R.success(this.templateService.deleteById(id));
    }

}

